﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using BCEntity.TMS.Driver.DriverExamine;
using BCCommon;
using System.Threading.Tasks;

namespace BCData.TMS.Driver.DriverExamine
{
    public class DriverExamineTestQuestionAnswerData : IDriverExamineTestQuestionAnswerData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_SQL = "SELECT A.DatumAnswerId,A.DatumAnswerId,A.TestQuestionNo,A.AnswerContent as TestquestionsAnswerContent,A.tIndex,B.AnswerContent,B.CreateTime,B.ModifyAdminId,B.ModifyDateTime  " +
                                        "FROM DriverExamineTestquestionAnswer A " +
                                        "LEFT JOIN  DriverLearnDatumAnswer B  ON A.DatumAnswerId=B.DatumAnswerId  ";

        public DriverExamineTestQuestionAnswerData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        /// <summary>
        /// 新增司机考试试题答案
        /// </summary>
        /// <param name="entity">司机考试试题答案</param>
        /// <returns></returns>
        public async Task<int> InsertAsync(DriverExamineTestQuestionAnswerEntity entity)
        {
            string sql = @"INSERT INTO DriverExamineTestquestionAnswer(TestquestionNo,DatumAnswerId,AnswerContent,tIndex)
                           VALUES(?TestquestionNo,?DatumAnswerId,?AnswerContent,?tIndex);SELECT LAST_INSERT_ID();";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?TestquestionNo", entity.TestquestionNo, DbType.AnsiString, size: 36);
            parameter.Add("?DatumAnswerId", entity.DatumAnswerId, DbType.Int32);
            parameter.Add("?AnswerContent", entity.AnswerContent, DbType.AnsiString, size: 200);
            parameter.Add("?tIndex", entity.TIndex, DbType.Int32);
            var result = await this.databaseContext.Master.ExecuteScalarAsync<int>(sql, parameter);
            return result;
        }

        /// <summary>
        /// 获取最大的考试题编号
        /// </summary>
        /// <returns></returns>
        public int GetMaxTestquestionNo()
        {
            string sql = @"SELECT DISTINCT(TestquestionNo) from  DriverExamineTestquestionAnswer;SELECT COUNT(1) FROM DriverExamineTestquestionAnswer;";

            var result = this.databaseContext.Master.QueryMultiple<DriverExamineTestQuestionAnswerEntity, int>(sql);

            var datas = result.Item1;

            var testquestionNos = datas.Select(s => s.TestquestionNo);
            List<int> temp = new List<int>();
            temp.Clear();
            foreach (var item in testquestionNos)
            {
                temp.Add(Convert.ToInt32(item));
            }
            if (temp.Any())
            {
                return temp.Max();
            }
            return default(int);
        }

        /// <summary>
        /// 获取考试题答案列表
        /// </summary>
        /// <param name="datumIds">学习资料Id列表</param>
        /// <returns></returns>
        public async Task<IEnumerable<DriverExamineTestQuestionAnswerEntity>> GetTestQuestionAnswerByDaturnAnswerIds(int[] datumIds)
        {
            string ids = string.Empty;

            DynamicParameters parameter = new DynamicParameters();
            if (datumIds != null && datumIds.Any())
            {
                ids = string.Join(",", datumIds);
            }
            string sql = string.Format(@"SELECT TestQuestionNo,DatumAnswerId,AnswerContent,tIndex from DriverExamineTestquestionAnswer WHERE DatumAnswerId in(SELECT DatumAnswerId from DriverLearnDatum Where DatumId in({0}));", ids);
            return await this.databaseContext.Master.QueryAsync<DriverExamineTestQuestionAnswerEntity>(sql);
        }

        /// <summary>
        /// 获取考试题答案
        /// </summary>
        /// <param name="paperId">试卷Id</param>
        /// <returns></returns>
        public async Task<IEnumerable<DriverExamineTestQuestionAnswerEntity>> GetDriverExamineTestQuestionAnswersByPaperId(long paperId)
        {
            string sql = string.Format(@"{0} Where A.TestQuestionNo in(SELECT TestQuestionNo from  DriverExamineTestquestion WHERE PaperId=?PaperId);", QUERY_SQL);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?PaperId", paperId, DbType.Int64);
            return await this.databaseContext.Master.QueryAsync<DriverExamineTestQuestionAnswerEntity>(sql, parameter);
        }

        /// <summary>
        /// 批量删除考试题答案
        /// </summary>
        /// <param name="testquestionNos">考试题编号列表</param>
        /// <returns></returns>
        public async Task<int> DeleteByTestquestionNos(string[] testquestionNos)
        {
            string questionsNos = string.Empty;
            if (testquestionNos != null && testquestionNos.Any())
            {
                questionsNos = string.Join(",", testquestionNos);
            }
            string sql = string.Format(@"DELETE from DriverExamineTestquestionAnswer WHERE  TestQuestionNo IN ({0})", string.Join(",", questionsNos));
            var result = await this.databaseContext.Master.ExecuteScalarAsync<int>(sql, CommandType.Text);
            return result;
        }

        /// <summary>
        /// 考试题答案列表
        /// </summary>
        /// <param name="testquestionNos">考试题编号列表</param>
        /// <returns></returns>
        public async Task<IEnumerable<DriverExamineTestQuestionAnswerEntity>> GetDriverExamineTestQuestionAnswersByQuestionsNos(string[] testquestionNos)
        {
            string questionsNos = string.Empty;
            if (testquestionNos != null && testquestionNos.Any())
            {
                questionsNos = string.Join(",", testquestionNos);
            }
            string sql = string.Format(@"SELECT TestquestionNo,DatumAnswerId,AnswerContent,TIndex from DriverExamineTestquestionAnswer WHERE  TestQuestionNo IN ({0});", string.Join(",", questionsNos));
            return await this.databaseContext.Master.QueryAsync<DriverExamineTestQuestionAnswerEntity>(sql);
        }
    }
}
